package api

import (
	"dianping_sol/db"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
)

func GetShopInfo(c *gin.Context) {
	shopIdC := c.Query("id")
	pagesizeC := c.Query("pagesize")
	pageC := c.Query("page")
	if shopIdC == "" && (pagesizeC != "" && pageC != "") { //查询分页
		pageSize, err := strconv.Atoi(pagesizeC)
		if err != nil {
			c.JSON(http.StatusOK, Err())
			return
		}
		page, err := strconv.Atoi(pageC)
		if err != nil {
			c.JSON(http.StatusOK, Err())
			return
		}

		var ms []db.Message
		err = db.G_DB.Debug().Limit(pageSize).Offset(pageSize * page).Find(&ms).Error
		if err != nil {
			c.JSON(http.StatusOK, Err())
			return
		}
		var count int64
		err = db.G_DB.Debug().Model(&db.Message{}).Count(&count).Error
		if err != nil {
			c.JSON(http.StatusOK, Err())
			return
		}
		var resp struct {
			Count    int64        `json:"count"`
			Messages []db.Message `json:"messages"`
		}

		resp.Messages = ms
		resp.Count = count
		c.JSON(http.StatusOK, Suc(resp))
		return
	}
	if shopIdC != "" && (pagesizeC == "" && pageC == "") { //查询单条数据
		var m db.Message
		err := db.G_DB.Where(`shop_id = ?`, strings.TrimSpace(shopIdC)).Find(&m).Error
		if err != nil {
			c.JSON(http.StatusOK, Err())
			return
		}
		if m.Id > 0 {
			c.JSON(http.StatusOK, Suc(m))
			return
		}
		c.JSON(http.StatusOK, Suc(`无该商户信息`))
		return

	}
	c.JSON(http.StatusOK, Err())
}
